<?php

namespace app\index\controller;

use think\facade\View;

class WeatherCity
{

    public function index()
    {
        $keyword = get_param('keyword');
        View::assign('keyword', $keyword);
        return View::fetch();
    }

    public function getList()
    {
        //接受参数
        $page = get_param('page');
        $limit = get_param('limit');
        $keyword = get_param('keyword');
        $weatherCityModel = new \app\common\model\WeatherCity();
        //构建查询条件
        $condition = array();
        if (!empty($keyword)) $condition[] = ['cityZh|provinceZh|leaderZh', 'like', '%' . $keyword . '%'];
        $data = $weatherCityModel->where($condition)->page($page)->limit($limit)->select();
        $count = $weatherCityModel->where($condition)->count();
        output_data_table($data, $count);
    }

    public function add()
    {
        $provinceZh= post_param('provinceZh');
        $leaderZh = post_param('leaderZh');
        $cityZh = post_param('cityZh');
        $lon = post_param('lon');
        $lat = post_param('lat');
        if (empty($cityZh)) return View::fetch();
       $weathercityModel=new \app\common\model\WeatherCity();
        //数据校验
        $data = $weathercityModel->where('cityZh', '=', $cityZh)->find();
        if (!empty($data)) output_error('此城市已存在~');
        //插入数据
        $data = array();
        $data['provinceZh'] = $provinceZh;
        $data['leaderZh'] = $leaderZh;
        $data['cityZh'] = $cityZh;
        $data['lon'] = $lon;
        $data['lat'] = $lat;
        $weathercityModel->insert($data);
        output_success();
        return null;
    }

    public function edit()
    {
        $provinceZh= post_param('provinceZh');
        $leaderZh = post_param('leaderZh');
        $cityZh = post_param('cityZh');
        $lon = post_param('lon');
        $lat = post_param('lat');
        $weathercityModel=new \app\common\model\WeatherCity();
        if (empty($cityZh) || empty($provinceZh) || empty($leaderZh)) {
            $id = get_param('id');
            if (empty($id)) output_error_param();
            $data = $weathercityModel->where('id', '=', $id)->find();
            if (empty ($data)) output_error_param();
            View::assign('data', $data);
            return View::fetch();
        }
        //提交后的处理
        $id = get_param('id');
        $data = $weathercityModel->where('id', '=', $id)->find();
        if (!empty($data) && $data['id'] != $id) output_error('此城市已存在~');
        //构建更新数据
        $update = array();
        $update['provinceZh'] = $provinceZh;
        $update['leaderZh'] = $leaderZh;
        $update['cityZh'] = $cityZh;
        $update['lon'] = $lon;
        $update['lat'] = $lat;
        //执行更新语句
        $weathercityModel->where('id', '=', $id)->update($update);
        output_success();
        return null;
    }

    public function modify()
    {
        //渲染页面
        $id = get_param('id');
        $id = substr($id, 0, strlen($id) - 1);
        $name = post_param('name');
        $value = post_param('value');
        if (empty($name) || empty($value)) {
            View::assign('id', $id);
            return View::fetch();
        }
        $id = explode(',', $id);
        $weathercityModel=new \app\common\model\WeatherCity();
        $update = array();
        $update[$name] = $value;
        $weathercityModel->where("id", 'in', $id)->update($update);
        output_success();
        return null;
    }

    public function batch()
    {
        //完善接口，实现批量删除
        $id = get_param('id');
        $id = substr($id, 0, strlen($id) - 1);
        $id = explode(',', $id);
      $weathercityModel=new \app\common\model\WeatherCity();
        $weathercityModel->where("id", 'in', $id)->delete();
        output_success();
    }

    public function delete()
    {
        $id = get_param('id');
  $weathercityModel=new \app\common\model\WeatherCity();
        $weathercityModel->where('id', '=', $id)->delete();
        output_success();
    }

}
